package com.deep.spring.v3;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.ModelAndView;

@ControllerAdvice
public class TestHandler {

//    @ExceptionHandler(CustomGenericException.class)
//    public String test(CustomGenericException ex) {
//        System.out.println("抛异常了:" + ex);
//        ex.printStackTrace();
//        String resultStr = "异常：默认";
//        return resultStr;
//    }


    @ExceptionHandler(CustomGenericException.class)//可以直接写@ExceptionHandler,不指明异常类，会自动映射
    public ModelAndView customGenericExceptionHnadler(CustomGenericException exception) { //还可以声明接收其他任意参数
        ModelAndView modelAndView = new ModelAndView("generic_error");
        modelAndView.addObject("errCode", exception.getErrCode());
        modelAndView.addObject("errMsg", exception.getErrMsg());

        System.out.println("aaaaaaaaaaaaaaa");
        return modelAndView;
    }

    @ExceptionHandler(Exception.class)//可以直接写@EceptionHandler，IOExeption继承于Exception
    public ModelAndView allExceptionHandler(Exception exception) {
        ModelAndView modelAndView = new ModelAndView("generic_error");
        modelAndView.addObject("errMsg", "this is Exception.class");
        return modelAndView;
    }
}
